import { math, SceneAsset, tween, Vec2, view } from "cc";
import * as fgui from "fairygui-cc";
import Instance from "../../../Core/Utils/Instance";
import { LayerManager } from "../../../Core/Layer/LayerManager";
import TimerMgr from "../../../Core/Timer/TimerMgr";
import UICore from "../../../Core/Res/UICore";
export class ViewManager {
    private _loading: fgui.GComponent;
    private _loadRanking: fgui.GComponent;
    public static get ins(): ViewManager {
        return Instance.get(ViewManager);
    }
    /**
      * 显示提示信息
      * @param str 提示信息内容，支持字符串和数字类型
      * @returns 无返回值
      */
    public showtips(str: string | number): void {
        const render: fgui.GComponent = UICore.createObject(`Common`, `TipsRender`).asCom;
        render.touchable = false;
        render.getChild("tips").text = str + "";
        const widthH: number = view.getVisibleSize().width;
        const heightH: number = view.getVisibleSize().height;
        render.x = (widthH - render.width) >> 1;
        render.y = ((heightH - render.height) >> 1) - 150;
        LayerManager.ins.tip.addChild(render);
        tween(render).to(2, { y: render.y - 100 }).call((data: fgui.GObject) => {
            data.removeFromParent();
            data.dispose();
        }).start();
    }
    public showloading(): void {
        if (!this._loading) {
            this._loading = UICore.createObject(`Common`, `LoadingView`).asCom;
            this._loading.setSize(LayerManager.ins.loading.width, LayerManager.ins.loading.height);
            this._loading.name = `loading`;
        }
        if (LayerManager.ins.loading.getChild(`loading`)) {
            return;
        }
        LayerManager.ins.loading.addChild(this._loading);
        TimerMgr.ins.add(30000, 1, this.removeLoading, this);
    }
    public removeLoading(): void {
        TimerMgr.ins.remove(this.removeLoading, this);
        if (this._loading) {
            LayerManager.ins.loading.removeChild(this._loading);
            this._loading.dispose();
            this._loading = null;
        }
    }

    public showRanging(callback?: Function): void {
        if (!this._loadRanking) {
            this._loadRanking = UICore.createObject(`Common`, `LoadRankView`).asCom;
            this._loadRanking.setSize(LayerManager.ins.loading.width, LayerManager.ins.loading.height);
            this._loadRanking.name = `ranking`;
        }
        (this._loadRanking.getChild(`cancel`) as fgui.GButton).visible = false;
        (this._loadRanking.getChild(`cancel`) as fgui.GButton).onClick(() => {
            this.removeRanging();
            callback && callback();
        });
        if (LayerManager.ins.loading.getChild(`ranking`)) {
            return;
        }
        LayerManager.ins.loading.addChild(this._loadRanking);
    }
    public removeRanging(): void {
        if (this._loadRanking) {
            LayerManager.ins.loading.removeChild(this._loadRanking);
            this._loadRanking.dispose();
            this._loadRanking = null;
        }
    }
}